
const testData = [
  { id: 1, name: 'DianaYako', phone: '13123456789', note: '测试Item - 1' },
  { id: 2, name: 'Elaina', phone: '13123456790', note: '测试Item - 2' },
  { id: 3, name: 'bilibili', phone: '13123456791', note: '测试Item - 3' },
  { id: 4, name: 'HOXIA', phone: '13123456792', note: '测试Item - 4' },
  { id: 5, name: 'Test - User - 2', phone: '13123456793', note: '测试Item - 5' },
  { id: 6, name: 'Test - User - 3', phone: '13123456794', note: '测试Item - 6' },
  { id: 7, name: 'Test - User - 4', phone: '13123456795', note: '测试Item - 7' },
  { id: 8, name: 'Test - User - 5', phone: '13123456796', note: '测试Item - 8' },
  { id: 9, name: 'Test - User - 6', phone: '13123456797', note: '测试Item - 9' },
  { id: 10, name: 'Test - User - 7', phone: '13123456798', note: '测试Item - 10' },
  { id: 11, name: 'Test - User - 8', phone: '13123456799', note: '测试Item - 11' },
  { id: 12, name: 'Test - User - 9', phone: '13123456800', note: '测试Item - 12' },
  { id: 13, name: 'Test - User - 0', phone: '13123456801', note: '测试Item - 13' },
  { id: 14, name: 'Test - User - 10', phone: '13123456802', note: '测试Item - 14' },
  { id: 15, name: 'Test - User - 11', phone: '13123456803', note: '测试Item - 15' }
]

export const getListDataApi = (data?: any) => new Promise<KeyValue>(resolve => {
  let page = undefined as any
  let condition = undefined as any
  if (data) {
    page = data.page
    condition = data.condition
  }
  let current = page?.current ? page.current : 1
  let size = page?.size ? page.size : 10
  let resData = {
    records: [] as any[],
    page: { current, size, total: testData.length }
  }

  let queryData = JSON.parse(JSON.stringify(testData))

  if (condition && condition.name) {
    queryData = queryData.filter((item: any) => item.name.includes(condition.name))
  }

  resData.records = queryData.slice((current - 1) * size, current * size)

  resolve({ code: 200, data: resData })
})

export const createOrUpdateApi = (data: any) => new Promise((resolve, reject) => {
  if (data) {
    if (data.id) {
      let index = testData.findIndex(item => item.id === data.id) 
      if (index === -1) {
        reject('请传入正确id')
      } else {
        testData.splice(index, 1, data)
        resolve({ code: 200, data: data.id })
      }
    } else {
      data.id = testData.length + 1
      testData.push(data)
      resolve({ code: 200, data: data.id })
    }
  } else {
    reject('无数据')
  }
})

export const deleteRowApi = (params: { id: number }) => new Promise((resolve, reject) => {
  if (!params?.id) {
    reject('请传入id')
  }

  let index = testData.findIndex(item => item.id === params.id)
  console.log(params.id)
  if (index === -1) {
    reject('找不到对应数据！')
  } else {
    testData.splice(index, 1)
    resolve({ code: 200, data: index })
  }
})